﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using System.ComponentModel;
using System.Text.Json.Serialization;

namespace Admin.NET.Application.Service.PlanViews.Dto;

public class GanttRequest
{
    public CycleCommonFilters Filters { get; set; } = new();
    public TimeFieldMode TimeField { get; set; } = TimeFieldMode.Both;
    public TimeMatchMode TimeMatch { get; set; } = TimeMatchMode.Intersects;
    public List<string> Dims { get; set; } = new();
    public List<string>? ParentKeyPath { get; set; }
    public int PageIndex { get; set; } = 1;
    public int PageSize { get; set; } = 50;
    public string LaneMode { get; set; } = "both"; // plan | actual | both
    public bool OnlyDelayed { get; set; } = false;
    public bool Lazy { get; set; } = false;
    /// <summary>
    /// 后端批量展开的目标深度（从1开始，1=第一层）。优先级：ExpandAll > ExpandDepth > Lazy。
    /// </summary>
    public int? ExpandDepth { get; set; }
    /// <summary>
    /// 是否展开全部层级（返回完整嵌套树）
    /// </summary>
    public bool ExpandAll { get; set; } = false;
    /// <summary>
    /// 合并维度键（如 ship、section），用于在该层返回 split 结构
    /// </summary>
    public string? CombineDim { get; set; }
    /// <summary>
    /// 合并模式（默认 split）
    /// </summary>
    public string CombineMode { get; set; } = "split";

    // 计划/实际 开工/完工范围（可选）
    public DateTime? PlanStartFrom { get; set; }
    public DateTime? PlanStartTo { get; set; }
    public DateTime? PlanEndFrom { get; set; }
    public DateTime? PlanEndTo { get; set; }
    public DateTime? ActualStartFrom { get; set; }
    public DateTime? ActualStartTo { get; set; }
    public DateTime? ActualEndFrom { get; set; }
    public DateTime? ActualEndTo { get; set; }
}

public class GanttItem
{
    public string Id { get; set; } = string.Empty;
    public string Label { get; set; } = string.Empty;
    public List<string>? KeyPath { get; set; }
    public DateTime? PlanStart { get; set; }
    public DateTime? PlanEnd { get; set; }
    public DateTime? ActualStart { get; set; }
    public DateTime? ActualEnd { get; set; }
    public decimal? Progress { get; set; }
    public string? Status { get; set; }
}

public class GanttLane
{
    public string Id { get; set; } = string.Empty;
    public string Label { get; set; } = string.Empty;
    public List<string> Path { get; set; } = new();
    public List<GanttItem> Items { get; set; } = new();
}

public class GanttResponse
{
    /// <summary>
    /// 分层节点（新版结构）
    /// </summary>
    public List<GanttHierarchyNode> Nodes { get; set; } = new();

    /// <summary>
    /// 兼容旧版字段（后续将移除）
    /// </summary>
    [Obsolete("请使用 Nodes 字段")]
    public List<GanttLane> Lanes { get; set; } = new();

    public DateTime TimeStart { get; set; }
    public DateTime TimeEnd { get; set; }
    public int TotalItems { get; set; }
    public int TotalNodes { get; set; }
}

public class GanttHierarchyNode
{
    public string Id { get; set; } = string.Empty;
    public string Label { get; set; } = string.Empty;
    public int Level { get; set; }
    public List<string> KeyPath { get; set; } = new();
    public List<string> DisplayPath { get; set; } = new();
    public List<GanttHierarchyNode> Children { get; set; } = new();
    public List<GanttItem> Items { get; set; } = new();
    public DateTime? PlanStart { get; set; }
    public DateTime? PlanEnd { get; set; }
    public DateTime? ActualStart { get; set; }
    public DateTime? ActualEnd { get; set; }
    public double? ProgressPercent { get; set; }
    public bool HasChildren { get; set; }
    /// <summary>
    /// 合并渲染标记（如 'split'）
    /// </summary>
    public string? Render { get; set; }
    /// <summary>
    /// 合并行的子段（用于 split）
    /// </summary>
    public List<GanttItem>? SplitChildren { get; set; }

    [JsonIgnore]
    public Dictionary<string, GanttHierarchyNode> ChildrenLookup { get; set; } = new();

    [JsonIgnore]
    internal double ProgressSum { get; set; }

    [JsonIgnore]
    internal int ProgressCount { get; set; }
}
